#include "stm32f10x.h"                  // Device header
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <led.h>
#include <oled.h>
#include <move.h>
#include <string.h>
#include "Delay.h"
#include "Serial.h"

/*******************************************************************************
* ? ? ?         : pwm_init
* ????		   : IO???TIM3?????	   
* ?    ?         : ?
* ?    ?         : ?
*******************************************************************************/
void pwm_init()
{
	GPIO_InitTypeDef GPIO_InitStructure;   //?????????,?????GPIO

	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;//?????????,????????

	TIM_OCInitTypeDef TIM_OCInitStructure;//??TIM_OCInitStruct???????????TIMx

	/* ???? */
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);

	/*  ??GPIO????IO? */
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;// PC6
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;//??????
	GPIO_Init(GPIOC,&GPIO_InitStructure);
	
	//TIM3??????
	TIM_TimeBaseInitStructure.TIM_Period = 199; //PWM ??=72000/(199+1)=36Khz//??????????????
	TIM_TimeBaseInitStructure.TIM_Prescaler = 7199;//??????TIMx????????
	TIM_TimeBaseInitStructure.TIM_ClockDivision = 0;//??????:TDTS = Tck_tim
	TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;	//TIM??????
	TIM_TimeBaseInit(TIM3, & TIM_TimeBaseInitStructure);

	GPIO_PinRemapConfig(GPIO_FullRemap_TIM3,ENABLE);//?????????	//pC6

	//PWM???	  //??TIM_OCInitStruct???????????TIMx
	TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;
	TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;//PWM????
	TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low;

	TIM_OC1Init(TIM3,&TIM_OCInitStructure);
	//????????TIM_OC1Init???TIM_OCInit,??????????????????
	TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);//??????TIMx?CCR1????????
	TIM_Cmd(TIM3,ENABLE);//??????TIMx??
}
